package com.sec.android.app.myfiles.widget;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.sec.android.app.myfiles.R;

/**
 * Created by daesu83.kim on 2016-10-13.
 */

public class EditTextEx extends EditText {

    private String mPostfix;

    public EditTextEx(Context context) {
        super(context);
    }

    public EditTextEx(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextEx(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setPostfix(String postfix) {
        mPostfix = postfix;
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);
        if (!TextUtils.isEmpty(mPostfix)) {
            int length = length();
            int postfixLength = mPostfix.length();
            int lastPosition = length - postfixLength;
            if (selEnd > lastPosition && length >= postfixLength) {
                setSelection(selStart == selEnd ? lastPosition : selStart, lastPosition);
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (!TextUtils.isEmpty(mPostfix)) {
            int start = getSelectionStart();
            int end = getSelectionEnd();
            if (start == end && (keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) && isLastPosition(end)) {
                View v = focusSearch(FOCUS_DOWN);
                if (v != null) {
                    v.requestFocus(FOCUS_FORWARD);
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private boolean isLastPosition(int pos) {
        String text = getText().toString();
        int textLength = 0;
        if (!TextUtils.isEmpty(text)) {
            textLength = text.length();
        }
        if (!TextUtils.isEmpty(mPostfix)) {
            int postFixLength = mPostfix.length();
            textLength = (textLength >= postFixLength) ? textLength - postFixLength : 0;
        }
        return textLength == pos;
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        if (!TextUtils.isEmpty(mPostfix) && !TextUtils.isEmpty(text)) {
            if (text.toString().endsWith(mPostfix)) {
                SpannableString span = new SpannableString(text);
                int postfixTextColor = getResources().getColor(R.color.winset_list_item_text1_disabled, null);
                int textLength = text.length();
                if (textLength - mPostfix.length() >= 0) {
                    span.setSpan(new ForegroundColorSpan(postfixTextColor),
                            textLength - mPostfix.length(), textLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                text = span;
            }
        }
        super.setText(text, type);
    }

}
